# core/phone_processor.py
from config.constants import END_PATTERNS

class PhoneProcessor:
    def __init__(self):
        pass

    def generate_phone_numbers(self, prefix_info, suffixes):
        """生成手机号码"""
        generated_numbers = []
        
        for prefix_entry in prefix_info:
            for suffix in suffixes:
                phone = f"{prefix_entry['phone']}{suffix}"
                generated_numbers.append({
                    "city": prefix_entry['city'],
                    "isp": prefix_entry['isp'],
                    "phone": phone,
                    "pattern": self.get_pattern(suffix)
                })
        
        return generated_numbers

    def get_pattern(self, suffix):
        """根据后缀识别号码格式"""
        return self._match_pattern(suffix)

    def _match_pattern(self, suffix):
        """匹配后缀格式"""
        # 确保后缀是字符串
        suffix = str(suffix)
        
        # 检查是否匹配已知格式
        for pattern, example in END_PATTERNS.items():
            if len(suffix) != len(example):
                continue
            
            # 特殊处理AAAA格式
            if pattern == "AAAA" and len(suffix) == 4:
                if suffix[0] == suffix[1] == suffix[2] == suffix[3]:
                    return pattern
            
            # 特殊处理AAA格式
            elif pattern == "AAA" and len(suffix) == 3:
                if suffix[0] == suffix[1] == suffix[2]:
                    return pattern
            
            # 特殊处理AAAB格式
            elif pattern == "AAAB" and len(suffix) == 4:
                if suffix[0] == suffix[1] == suffix[2] and suffix[2] != suffix[3]:
                    return pattern
            
            # 特殊处理AABA格式
            elif pattern == "AABA" and len(suffix) == 4:
                if suffix[0] == suffix[1] and suffix[1] != suffix[2] and suffix[2] != suffix[3] and suffix[0] == suffix[3]:
                    return pattern
            
            # 特殊处理AABB格式
            elif pattern == "AABB" and len(suffix) == 4:
                if suffix[0] == suffix[1] and suffix[2] == suffix[3] and suffix[1] != suffix[2]:
                    return pattern
            
            # 特殊处理AABC格式
            elif pattern == "AABC" and len(suffix) == 4:
                if suffix[0] == suffix[1] and suffix[1] != suffix[2] and suffix[2] != suffix[3]:
                    return pattern
            
            # 特殊处理ABAA格式
            elif pattern == "ABAA" and len(suffix) == 4:
                if suffix[0] != suffix[1] and suffix[1] != suffix[2] and suffix[2] == suffix[3] and suffix[0] == suffix[2]:
                    return pattern
            
            # 特殊处理ABAB格式
            elif pattern == "ABAB" and len(suffix) == 4:
                if suffix[0] == suffix[2] and suffix[1] == suffix[3] and suffix[0] != suffix[1]:
                    return pattern
            
            # 特殊处理ABAC格式
            elif pattern == "ABAC" and len(suffix) == 4:
                if suffix[0] == suffix[2] and suffix[1] != suffix[3] and suffix[0] != suffix[1]:
                    return pattern
            
            # 特殊处理ABBA格式
            elif pattern == "ABBA" and len(suffix) == 4:
                if suffix[0] == suffix[3] and suffix[1] == suffix[2] and suffix[0] != suffix[1]:
                    return pattern
            
            # 特殊处理ABBC格式
            elif pattern == "ABBC" and len(suffix) == 4:
                if suffix[1] == suffix[2] and suffix[0] != suffix[1] and suffix[2] != suffix[3]:
                    return pattern
            
            # 特殊处理ABCA格式
            elif pattern == "ABCA" and len(suffix) == 4:
                if suffix[0] == suffix[3] and suffix[1] != suffix[2] and suffix[0] != suffix[1]:
                    return pattern
            
            # 特殊处理ABCB格式
            elif pattern == "ABCB" and len(suffix) == 4:
                if suffix[1] == suffix[3] and suffix[0] != suffix[1] and suffix[1] != suffix[2]:
                    return pattern
            
            # 特殊处理ABCC格式
            elif pattern == "ABCC" and len(suffix) == 4:
                if suffix[2] == suffix[3] and suffix[0] != suffix[1] and suffix[1] != suffix[2]:
                    return pattern
            
            # 特殊处理ABCD格式
            elif pattern == "ABCD" and len(suffix) == 4:
                if suffix[0] != suffix[1] and suffix[1] != suffix[2] and suffix[2] != suffix[3]:
                    return pattern
        
        return "普通号码"